INCLUDE = -I
DEFINE  = -D

CHIP_RK3399 =
CHIP_RV1126 = y

ifeq ($(CHIP_RK3399),y)
  TOOLCHAIN_PATH = $(RK3399_TOOLCHAIN_PATH)
  SYSROOT = $(RK3399_SYSROOT)
  CC = $(TOOLCHAIN_PATH)/bin/aarch64-linux-gnu-gcc
  AS = $(TOOLCHAIN_PATH)/bin/aarch64-linux-gnu-as
  CXX = $(TOOLCHAIN_PATH)/bin/aarch64-linux-gnu-g++
  AR = $(TOOLCHAIN_PATH)/bin/aarch64-linux-gnu-ar
  LD = $(TOOLCHAIN_PATH)/bin/aarch64-linux-gnu-ld
endif

ifeq ($(CHIP_RV1126),y)
  TOOLCHAIN_PATH = $(RV1126_TOOLCHAIN_PATH)/gcc-arm-8.3-2019.03-x86_64-arm-linux-gnueabihf
  SYSROOT = $(RV1126_SYSROOT)
  CC = $(TOOLCHAIN_PATH)/bin/arm-linux-gnueabihf-gcc
  AS = $(TOOLCHAIN_PATH)/bin/arm-linux-gnueabihf-as
  CXX = $(TOOLCHAIN_PATH)/bin/arm-linux-gnueabihf-g++
  AR = $(TOOLCHAIN_PATH)/bin/arm-linux-gnueabihf-ar
  LD = $(TOOLCHAIN_PATH)/bin/arm-linux-gnueabihf-ld
endif

INCDIR = $(SYSROOT)/usr/include/

SRCDIR = src

LIBS = $(SYSROOT)/usr/lib

PREDEFINED :=

S_SRCS_FILES := $(foreach src_dir,$(SRCDIR),$(wildcard $(src_dir)/*.S))
BUILD_DIRS := $(foreach file,$(S_SRCS_FILES),build/$(dir $(file))) bin

TEMP1  := $(addprefix build/,$(S_SRCS_FILES))
S_OBJS := $(patsubst %.S,%.o,$(TEMP1))

TARGET = $(addprefix bin/,$(patsubst %.S,%,$(notdir $(S_SRCS_FILES))))

CFLAGS += \
	$(addprefix $(INCLUDE),$(INCDIR)) \
	$(addprefix $(DEFINE),$(PREDEFINED))

LINK_FLAGS += $(CFLAGS) \
			  $(addprefix -L,$(LIBS))

all: mk_dirs $(TARGET)

mk_dirs:
	@$(foreach d,$(BUILD_DIRS),$(shell mkdir -p $(d)))

build/%.o: %.S
	@echo "    AS $<"
	@$(AS) $(CFLAGS) -c $< -o $@

$(TARGET): $(S_OBJS)
	@echo "    LINK ALL"
	@$(foreach s,$(S_OBJS),$(shell $(LD) $(s) $(LINK_FLAGS) -o $(addprefix bin/,$(patsubst %.o,%,$(notdir $(s))))))

clean:
	@rm -rf bin/ build/
